// index.js
Page({
  data: {
    loginDisabled: false,
    nowLocation: '暂无定位信息'
  },

  onLoad: function (event) {
    // 确定登录态
    if (getApp().globalData.wxusername == '') {
      this.setData({
        loginDisabled: true
      })
    }
    // 确定当前位置
    this.getNowLocation();
  },

  onShow: function (event) {
    // 确定登录态
    if (getApp().globalData.wxusername == '') {
      this.setData({
        loginDisabled: true
      })
    }
    // 确定当前位置
    this.getNowLocation();
  },

  onHide: function (event) {
    // 确定登录态
    if (wx.getStorageSync('nickName') != null) {
      this.setData({
        loginDisabled: false
      })
    }
    // 确定当前位置
    this.getNowLocation();
  },


  // 登录
  userLogin: function (event) {
    // 获取用户基本信息
    const that = this
    wx.getUserProfile({
      desc: '用于登录',
      success: res => {
        // 在成功回调函数中处理获取用户信息的逻辑
        var nickName = res.userInfo.nickName;
        var avatarUrl = res.userInfo.avatarUrl;
        // 进行后续操作
        // 获取js_code
        wx.login({
          success: (res) => {
            wx.request({
              url: getApp().globalData.commonIP + '/wxuser/login',
              method: 'POST',
              data: {
                wxcode: res.code,
                nickName: nickName,
                avatarUrl: avatarUrl
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                if (res.data.msg == '登录成功') {
                  wx.setStorageSync('token', res.data.data.token)
                  wx.setStorageSync('nickName', nickName)
                  wx.setStorageSync('avatarUrl', avatarUrl)
                  that.setData({
                    loginDisabled: false,
                    userName: nickName,
                    userAvatar: avatarUrl
                  })
                  getApp().globalData.wxusername = nickName
                  getApp().globalData.wxuseravatar = avatarUrl
                } else {
                  wx.showModal({
                    title: '登录失败',
                    content: res.data.msg,
                    showCancel: false
                  });
                }
              }
            })
          }
        })
      }
    })
  },


  goGetRewards: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: "/pages/rewards/index"
    })
  },

  goLocation: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/location/index'
    })
  },

  goParkingConfusedly: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/parkingConfusedly/index'
    })
  },

  goBikeSiltation: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/bike-siltation/index'
    })
  },

  goApplyDispatch: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/apply-dispatch/index'
    })
  },

  goBikeDispatch: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    // 判断用户的类型是否是工作人员
    wx.request({
      url: getApp().globalData.commonIP + '/wxuser/getUserTypeByNickName',
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName'),
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.data == '工作人员') {
          wx.navigateTo({
            url: '/pages/bike-dispatch/index'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '您不属于工作人员，无权查看！',
            showCancel: false
          })
        }
      }
    })
  },

  goRequestProcess: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/request-process/index'
    })
  },

  goFeedback: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/feedback/index'
    })
  },

  getNowLocation() {
    // 确定当前位置
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/',
          data: {
            key: '6OSBZ-7ZSW4-UMWU5-K7X6V-VIGYT-W5F7V',
            location: that.data.latitude + ',' + that.data.longitude
          },
          success: function (res) {
            that.setData({
              nowLocation: res.data.result.address
            })
          }
        });
      }
    })
  }
})